#include "QKinectException.h"
#include <Windows.h>
#include <NuiApi.h>
#include <QObject>

/**
 * @fn	QKinectException::QKinectException (long errCode)
 *
 * @brief	Constructor.%ip%
 *
 * @author	Dengnc
 * @date	2012/11/10
 *
 * @param	errCode	The error code.
 */

QKinectException::QKinectException (long errCode) : _errCode (errCode)
{
	if (_msgMap.empty ()) {
		_msgMap.insert (std::make_pair (E_OUTOFMEMORY,
				QObject::tr ("Out of memory")));
		_msgMap.insert (std::make_pair (E_UNEXPECTED,
				QObject::tr ("Unexpected error occurred")));
		_msgMap.insert (std::make_pair (E_POINTER,
				QObject::tr ("The pointer is invalid")));
		_msgMap.insert (std::make_pair (E_INVALIDARG,
				QObject::tr ("The argument is invalid")));
		_msgMap.insert (std::make_pair (E_NUI_DEVICE_NOT_CONNECTED,
				QObject::tr ("The Kinect is not connected")));
		_msgMap.insert (std::make_pair (E_NUI_DEVICE_NOT_READY,
				QObject::tr ("The Kinect is not ready")));
		_msgMap.insert (std::make_pair (E_NUI_ALREADY_INITIALIZED,
				QObject::tr ("The Kinect has already been initialized")));
		_msgMap.insert (std::make_pair (E_NUI_NO_MORE_ITEMS,
				QObject::tr ("No more items")));
		_msgMap.insert (std::make_pair (E_NUI_FRAME_NO_DATA,
				QObject::tr ("No data in the frame")));
		_msgMap.insert (std::make_pair (E_NUI_STREAM_NOT_ENABLED,
				QObject::tr ("The stream has not been enabled")));
		_msgMap.insert (std::make_pair (E_NUI_IMAGE_STREAM_IN_USE,
				QObject::tr ("The image stream is in use")));
		_msgMap.insert (std::make_pair (E_NUI_FRAME_LIMIT_EXCEEDED,
				QObject::tr ("The frame limit is exceeded")));
		_msgMap.insert (std::make_pair (E_NUI_FEATURE_NOT_INITIALIZED,
				QObject::tr ("The feature has not been initialized")));
		_msgMap.insert (std::make_pair (E_NUI_DATABASE_NOT_FOUND,
				QObject::tr ("The NUI database is not found")));
		_msgMap.insert (std::make_pair (E_NUI_DATABASE_VERSION_MISMATCH,
				QObject::tr ("The version of NUI database doesn't match")));
		_msgMap.insert (std::make_pair (E_NUI_NOTCONNECTED,
				QObject::tr ("The Kinect is not connected")));
		_msgMap.insert (std::make_pair (E_NUI_NOTREADY,
				QObject::tr ("The Kinect is not ready")));
		_msgMap.insert (std::make_pair (E_NUI_SKELETAL_ENGINE_BUSY,
				QObject::tr ("The skeletal engine is busy")));
		_msgMap.insert (std::make_pair (E_NUI_NOTPOWERED,
				QObject::tr ("The Kinect is not powered")));
		_msgMap.insert (std::make_pair (E_NUI_BADIINDEX,
				QObject::tr ("The index of Kinect is invalid")));
	}
	std::map< long, QString >::iterator it = _msgMap.find (errCode);
	if (it == _msgMap.end ())
		_errMsg = _msgMap[E_UNEXPECTED];
	else
		_errMsg = it->second;
}

QString QKinectException::getMessage () const
{
	return _errMsg;
}

/**
 * @fn	long QKinectException::getErrCode () const
 *
 * @brief	Gets error code.%ip%
 *
 * @author	Dengnc
 * @date	2012/11/10
 *
 * @return	The error code.
 */

long QKinectException::getErrCode () const
{
	return _errCode;
}

QString QKinectException::getReason () const throw ()
{
	return _errMsg;
}

/**
 * @brief	The kinect exception message map.
 */

std::map< long, QString > QKinectException::_msgMap;